package com.davidlife.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor1 implements HandlerInterceptor {
    /**
     * 预处理，controller方法执行前执行
     * return true：放行，执行下一个连接器，如果没有，执行controller中的方法
     * return false：不放行，可以使用request或response跳转到一个页面来提示信息，如没权限等
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor1 preHandle 执行了... 111");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response);
        System.out.println("MyInterceptor1 postHandle 执行了... 111");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor1 afterCompletion 执行了... 111");
    }
}
